package com.atguigu.jxc.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.atguigu.jxc.dao.DamageListGoodsDao;
import com.atguigu.jxc.entity.DamageList;
import com.atguigu.jxc.entity.DamageListGoods;
import com.atguigu.jxc.service.DamageListGoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.alibaba.fastjson.JSON;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: fqy
 * Date: 2021/10/18
 * Time: 16:22
 * Describe:
 */
@Service
public class DamageListGoodServiceImpl implements DamageListGoodService {

    @Autowired
    private DamageListGoodsDao damageListGoodsDao;

    // 保存报损的商品
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public int saveDamageListGoods(DamageList damageList, String damageListGoodsStr) {
        //  将插入 t_damage_list （damageList主键damage_list_id自增）
        System.out.println("damageList.getDamageListId() = " + damageList.getDamageListId());
//        int i = damageListGoodsDao.insertDamageList(damageList);
        int i = damageListGoodsDao.insertDamageList(damageList);
        System.out.println("damageList.getDamageListId() = " + damageList.getDamageListId());
        //  将 damageListGoodsStr（damage_list_goods_id主键自增） 插入 t_damage_list_goods
        //  前端没有传入damage_list_id怎么整？
        //  先查然后传给damageList
        //  将Json对象->数组类型转换为 Json字符串
        String damageListGoodsJsonStr = damageListGoodsStr.replace("[", "").replace("]", "");
        //  将Json字符串 转换为 JavaBean对象
        DamageListGoods damageListGoods = JSON.parseObject(damageListGoodsJsonStr, DamageListGoods.class);

        int j =  damageListGoodsDao.insertDamageListGoods(damageList,damageListGoods);
        if (i == 0 || j==0){
            // 插入失败
            return 0;
        }
        // 插入成功
        return 1;
    }
    // 根据日期范围查询报损单
    @Override
    public Map<String, Object> getDamageListByDate(String sTime, String eTime) {
        Map<String, Object> map = new HashMap<>();
        List<DamageList> damageListByDate = damageListGoodsDao.selectDamageListByDate(sTime,eTime);
        map.put("rows", damageListByDate);
        return map;
    }
    // 根据报损单Id查询报损单商品信息
    @Override
    public Map<String, Object> getDamageListGoodsById(Integer damageListId) {
        Map<String, Object> map = new HashMap<>();
        List<DamageListGoods> damageListGoods = damageListGoodsDao.selectDamageListGoodsById(damageListId);
        map.put("rows",damageListGoods);
        return map;
    }
}
